package cannot.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cannot.pojo.History;
import cannot.pojo.MainType;
import cannot.pojo.OtherType;
import cannot.pojo.ShowIndex;
import cannot.pojo.ShowIndexImage;
import cannot.pojo.Tv;
import cannot.pojo.User;
import cannot.pojo.UserDetail;
import cannot.service.HistoryService;
import cannot.service.MainTypeService;
import cannot.service.OtherTypeService;
import cannot.service.PraiseService;
import cannot.service.ShowIndexImageService;
import cannot.service.ShowIndexService;
import cannot.service.TvService;
import cannot.service.UserDetailService;
import cannot.service.UserService;
import cannot.util.StoragePlace;

/**
 * 统管前台跳转的控制器
 * @author Administrator
 *
 */
@Controller
public class NavForeIndexController {
	String videoPath = StoragePlace.basicPlace+"tv"+File.separator;
	@Autowired
	ShowIndexImageService showIndexImageService;
	@Autowired
	TvService tvService;
	@Autowired
	ShowIndexService showIndexService;
	@Autowired
	MainTypeService mainTypeService;
	@Autowired
	OtherTypeService otherTypeService;
	@Autowired
	PraiseService praiseService;
	@Autowired
	UserDetailService userDetailService;
	@Autowired
	HistoryService historyService;
	@Autowired
	UserService userService;
	
	/**
	 * 前台首页跳转
	 */
	@RequestMapping("fore_indexPage")
	public String AdminSubCategoryPage(Model model,HttpServletRequest request){
		//所有主分类信息
		List<MainType> mainTypes = mainTypeService.listMainType();
		//首页轮播图信息
		List<ShowIndexImage> showIndexImages = showIndexImageService.showIndexImages();
		for(ShowIndexImage showIndexImage:showIndexImages){
			Tv tv = tvService.selectById(showIndexImage.getTv_id());
			showIndexImage.setTv(tv);
		}
		//需要加载的分类信息
		List<ShowIndex> showIndexs = showIndexService.listShowIndexs();
		for(ShowIndex showIndex:showIndexs){
			MainType mainType = mainTypeService.selectById(showIndex.getMaintype());
			showIndex.setMainType(mainType);
		}
		model.addAttribute("mainTypes", mainTypes);
		model.addAttribute("showIndexImages", showIndexImages);
		model.addAttribute("showIndexs", showIndexs);
		
		String name = "";
		String password = "";
		Cookie[] cookies = request.getCookies();
		for(Cookie cookie:cookies){
			if(cookie.getName().equals("namecookie")){
				name = cookie.getValue();
			}
			if(cookie.getName().equals("passwordcookie")){
				password = cookie.getValue();
			}
		}
		if(name!=""){
			Subject subject = SecurityUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(name, password);
			try {
				subject.login(token);
				Session session = subject.getSession();
				session.setAttribute("subject", subject);
				//设置有效时间
				SecurityUtils.getSubject().getSession().setTimeout(1800000);
				User u = userService.getByName(subject.getPrincipal().toString());
				UserDetail userDetail= userDetailService.getByForeginKey(u.getId());
				u.setUserDetail(userDetail);
				u.setSalt(null);
				u.setPassword(null);
				session.setAttribute("user", u);
			}catch (AuthenticationException e) {
				return "fore/foreIndex";
			}
		}
		return "fore/foreIndex";
	}


	
	
	/**
	 * 影片播放页面
	 */
	@RequestMapping("palytvPage")
	public String playTvPage(Integer id,Model model,HttpSession session){
		Tv tv = tvService.selectById(id);
		//播放次数+1
		if(tv.getPlay_times()==null){
			tv.setPlay_times(0);
		}
		tv.setPlay_times(tv.getPlay_times()+1);
		tvService.updateTv(tv);
		//加载猜你喜欢
		List<Tv> tvsLikeGuess = tvService.slelectTvLikeById(id);
		/**
		 * 遍历猜你喜欢
		 */
		model.addAttribute("tvsLikeGuess", tvsLikeGuess);
		model.addAttribute("tvid", id);
		model.addAttribute("playTimes", tv.getPlay_times());
		model.addAttribute("tv", tv);
		//点赞次数
		model.addAttribute("praiseTimes", praiseService.selectCountById(id));
		//如果已经登录了，就增加到对应角色的历史记录中
		User u = (User) session.getAttribute("user");
		if(u!=null){
			History history = new History();
			history.setTv_id(id);
			history.setUid(u.getId());
			historyService.addHistoryByTrue(history);
		}
		
		return "fore/foreTvPlayPage";
	}
	
	/**
	 * 登录页面
	 */
	@RequestMapping("loginPage")
	public String loginPage(){
		return "fore/foreLogin";
	}
	
	/**
	 * 跳转到用户中心页面
	 */
	@RequestMapping("userCenterPage")
	public String loadUserCenterPage(HttpSession session){
		return "usercenter/foreUserCenterPage";
	}
	
	/**
	 * 主分类：跳转到分类页面
	 */
	@RequestMapping("listCategoryPage")
	public String listCategory(Integer id,Model model){
		List<MainType> mainTypes = mainTypeService.listMainType();
		MainType mainType = mainTypeService.selectById(id);
		List<OtherType> otherTypes = otherTypeService.listByMainType(id);
		model.addAttribute("mainTypes", mainTypes);
		model.addAttribute("otherTypes", otherTypes);
		model.addAttribute("mainType", mainType);
		return "fore/foreCategory";
	}
	
	
	/**
	 * 子分类：跳转到分类页面
	 */
	@RequestMapping("listOtherCategoryPage")
	public String listOtherCategoryPage(Integer id,Model model){
		//通过子分类id找到主分类id
		OtherType otherType = otherTypeService.selectOtherType(id);
		List<MainType> mainTypes = mainTypeService.listMainType();
		MainType mainType = mainTypeService.selectById(otherType.getForeginkey());
		List<OtherType> otherTypes = otherTypeService.listByMainType(mainType.getId());
		model.addAttribute("mainTypes", mainTypes);
		model.addAttribute("otherTypes", otherTypes);
		model.addAttribute("mainType", mainType);
		model.addAttribute("otherType", otherType);
		return "fore/foreCategory";
	}
	
	/**
	 * 对listTv进行内容处理
	 */
	public List<Tv> subStringContent(List<Tv> tvs,int length){
		
		for(Tv t:tvs){
			if(t.getDescri().length()>length){
				t.setDescri(t.getDescri().substring(0,length)+"...");
			}
		}
		return tvs;
	}
}
